#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mystring.h"
#include "Codification.h"
#define WRDCMP( word ) !strncmp(Commands.StringsArray[i], word, sizeof(word) - 1)

int main()
{
    char filename[100] = {};
    gets(filename);
    struct text Commands = FileToText(filename);
    double* CommandsArray = (double*) calloc(Commands.Lines*2, sizeof(double));
    int k = 0;
    for (int i = 0; i < Commands.Lines; i++)
    {
        if (WRDCMP("add"))
        {
            CommandsArray[k] = ADD;
            k++;
        }
        else if (WRDCMP("mul"))
        {
            CommandsArray[k] = MUL;
            k++;
        }
        else if (WRDCMP("sub"))
        {
            CommandsArray[k] = SUB;
            k++;
        }
        else if (WRDCMP("div"))
        {
            CommandsArray[k] = DIV;
            k++;
        }
        else if (WRDCMP("sqrt"))
        {
            CommandsArray[k] = SQRT;
            k++;
        }
        else if (WRDCMP("sqr"))
        {
            CommandsArray[k] = SQR;
            k++;
        }
        else if(WRDCMP("push ax"))
        {
            CommandsArray[k] = PUSHAX;
            k++;
        }
        else if (WRDCMP("pop ax"))
        {
            CommandsArray[k] = POPAX;
            k++;
        }
        else if(WRDCMP("push bx"))
        {
            CommandsArray[k] = PUSHBX;
            k++;
        }
        else if (WRDCMP("pop bx"))
        {
            CommandsArray[k] = POPBX;
            k++;
        }
        else if(WRDCMP("push cx"))
        {
            CommandsArray[k] = PUSHCX;
            k++;
        }
        else if (WRDCMP("pop cx"))
        {
            CommandsArray[k] = POPCX;
            k++;
        }
        else if(WRDCMP("push dx"))
        {
            CommandsArray[k] = PUSHDX;
            k++;
        }
        else if (WRDCMP("pop dx"))
        {
            CommandsArray[k] = POPDX;
            k++;
        }
        else if (WRDCMP("push"))
        {
            double c = strtod(Commands.StringsArray[i] + sizeof("push"), NULL);
            CommandsArray[k] = PUSH;
            k++;
            CommandsArray[k] = c;
            k++;
        }
        else if (WRDCMP("pop"))
        {
            CommandsArray[k] = POP;
            k++;
        }
        else if (WRDCMP("jump"))
        {
            int d = atoi(Commands.StringsArray[i] + sizeof("jump") + 1);
            CommandsArray[k] = JUMP;
            k++;
            CommandsArray[k] = d + 100;
            k++;
        }
        else if (WRDCMP("jbe"))
        {
            int d = atoi(Commands.StringsArray[i] + sizeof("jbe") + 1);
            CommandsArray[k] = JBE;
            k++;
            CommandsArray[k] = d + 100;
            k++;
        }
        else if (WRDCMP("jae"))
        {
            int d = atoi(Commands.StringsArray[i] + sizeof("jae") + 1);
            CommandsArray[k] = JAE;
            k++;
            CommandsArray[k] = d + 100;
            k++;
        }
        else if (WRDCMP("jne"))
        {
            int d = atoi(Commands.StringsArray[i] + sizeof("jne") + 1);
            CommandsArray[k] = JNE;
            k++;
            CommandsArray[k] = d + 100;
            k++;
        }
        else if (WRDCMP("je"))
        {
            int d = atoi(Commands.StringsArray[i] + sizeof("je") + 1);
            CommandsArray[k] = JE;
            k++;
            CommandsArray[k] = d + 100;
            k++;
        }
        else if (WRDCMP("ja"))
        {
            int d = atoi(Commands.StringsArray[i] + sizeof("ja") + 1);
            CommandsArray[k] = JA;
            k++;
            CommandsArray[k] = d + 100;
            k++;
        }
        else if (WRDCMP("jb"))
        {
            int d = atoi(Commands.StringsArray[i] + sizeof("jb") + 1);
            CommandsArray[k] = JB;
            k++;
            CommandsArray[k] = d + 100;
            k++;
        }
        else if (WRDCMP("out"))
        {
            CommandsArray[k] = OUT;
            k++;
        }
        else if (WRDCMP(":"))
        {
            int d = atoi(Commands.StringsArray[i] + 1);
            CommandsArray[k] = d + 100;
            k++;
        }
        else if (WRDCMP("call"))
        {
            int d = atoi(Commands.StringsArray[i] + sizeof("call") + 1);
            CommandsArray[k] = CALL;
            k++;
            CommandsArray[k] = d + 100;
            k++;
        }
        else if (WRDCMP("ret"))
        {
            CommandsArray[k] = RET;
            k++;
        }
    }
    free(Commands.String);
    free(Commands.StringsArray);

    char resfilename[100] = {'a','s','m','\0'};
    FILE* result = fopen(strcat(resfilename, filename), "w");
    for (int i = 0; i < k; i++)
    {
        if (CommandsArray[i] == PUSH)
        {
            fprintf(result, "%lg %lg\n", CommandsArray[i], CommandsArray[i + 1]);
            i++;
        }
        else if(CommandsArray[i] == JUMP || CommandsArray[i] == JBE || CommandsArray[i] == JNE || CommandsArray[i] == JE ||
                CommandsArray[i] == JB || CommandsArray[i] == JA || CommandsArray[i] == JAE || CommandsArray[i] == CALL)
        {
            fprintf(result, "%lg :%lg\n", CommandsArray[i], CommandsArray[i + 1] - 100);
            i++;
        }
        else
        {
            if (CommandsArray[i] < 100) fprintf(result, "%lg\n", CommandsArray[i]);
            else fprintf(result, ":%lg\n", CommandsArray[i] - 100);
        }
    }
    fprintf(result, "%d\n", 0);
    free(result);
    free(CommandsArray);
    return 0;
}


#undef WRDCMP
